/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpInfo;
import com.jims.master.modules.csm.service.ExpInfoService;
import com.jims.master.modules.sys.entity.SysOrgDict;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 消耗品基本信息Controller
 * @author CTQ
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expInfo")
public class ExpInfoController extends BaseController {

	@Autowired
	private ExpInfoService expInfoService;

	@ModelAttribute
	public ExpInfo get(@RequestParam(required=false) String id) {
		ExpInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expInfoService.get(id);
		}
		if (entity == null){
			entity = new ExpInfo();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpInfo expInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpInfo> page = expInfoService.findPage(new Page<ExpInfo>(request, response), expInfo);
		model.addAttribute("page", page);
		return "modules/csm/expInfoIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpInfo expInfo, Model model) {
		model.addAttribute("expInfo", expInfo);
		return "modules/csm/expInfoForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpInfo expInfo, Model model) {
	    StringData data=new StringData();
		int i=expInfoService.save(expInfo);
        if(i > 0){
            data.setCode("success");
            data.setData("保存消耗品基本信息成功");
        }else if(i == -1){
            data.setCode("error");
            data.setData("该物品相关信息已经存在,不能重复维护!");
        }else{
            data.setCode("error");
            data.setData("保存消耗品基本信息失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpInfo expInfo) {
	    StringData data=new StringData();
		int i=expInfoService.delInfo(expInfo);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品基本信息成功");
        }else if(i==-1){
			data.setCode("info");
			data.setData("该消耗品已有消耗品价格，不能删除");
		}else{
            data.setCode("error");
            data.setData("删除消耗品基本信息失败");
        }
		return data;
	}

	/**
	 * @param expInfo 传递参数
	 * @return List<ExpDict>    返回类型
	 * @throws
	 * @Title: autoComplete
	 * @Desciption: (消耗品基本信息添加消耗品字典自动补全)
	 * @author CTQ
	 * @date 2017-03-02
	 */
	@RequiresPermissions("user")
	 @RequestMapping(value = {"autoComplete", ""})
	 @ResponseBody
	 public List<ExpInfo> autoComplete(String q,String limit, ExpInfo expInfo) {
		if(q!=null&&!q.equals("")){
			try {
				q = URLDecoder.decode(q,"UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		List<ExpInfo> list = expInfoService.findAutoCompleteList(expInfo,"",q,limit);
		return list;
	}

    /**
     * @param expInfo 传递参数
     * @return List<ExpInfo>    返回类型
     * @throws
     * @Title: autoCompletePrice
     * @Desciption: (消耗品入库选存在价格的消耗品自动补全)
     * @author ZHUQ
     * @date 2017-03-12
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoCompletePrice", ""})
    @ResponseBody
    public List<ExpInfo> autoCompletePrice(String q,String limit, ExpInfo expInfo) {
        if(q!=null&&!q.equals("")){
            try {
                q = URLDecoder.decode(q,"UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpInfo> list = expInfoService.findAutoCompletePriceist(expInfo, UserUtils.getOrgId(), q, limit);
        return list;
    }

}